package com.game.logic.core.scene.entry;

import com.coment.cfg.luban.unity.map.Vector2;
import com.game.logic.core.scene.ChannelEntry;
import com.xs.build.server.websocket.channel.ClientManager;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.util.function.Function;

/// 实体的二维地址
@Slf4j
@Getter
@ToString
@EqualsAndHashCode(callSuper = false)
public class Vector2Entry<T>
        extends ChannelEntry
        implements MapEntry<T>
{
    /// 坐标X
    private float x;

    /// 坐标Y
    private float y;

    /// 实体
    @Setter
    private T entry;

    /// 状态 1:新加入 2:离开
    public int status;

    public Vector2Entry(
            T entry,
            String channelId,
            Function<Object, ?> pushToGroup
    ) {
        super(channelId, pushToGroup);
        this.entry = entry;
        this.status = 1;
    }

    /// 移动
    @Override
    public void move(float x, float y) {
        float x1 = this.x + x;
        float y1 = this.y + y;
        log.debug("移动  [{},{}] ---> [{},{}]", x, y, this.x, this.y);
        this.update(x1, y1, entry);
    }

    /// 进入某个区域
    @Override
    public void join(Vector2 Vector2) {
        float x = (float) (Vector2.x + Math.random() * 0.1f);
        float y = (float) (Vector2.y + Math.random() * 0.1f);
        log.debug("进入某个区域  [{},{}]", x, y);
        this.update(x, y, entry);
    }

    /// 随机一个区域
    @Override
    public void randomVector(float... maxValue) {
        float x = (float) (Math.random() * maxValue[0]);
        float y = (float) (Math.random() * maxValue[1]);
        log.debug("随机一个区域  [{},{}]", x, y);
        this.update(x, y, entry);
    }

    /// 更新实体
    @Override
    public void update(float x, float y, T entry) {
        this.x = x;
        this.y = y;
        this.entry = entry;
    }


    /// 给实体推送信息
    @Override
    public void push(Object value) {
        //  获取当前虚拟线程的附加信息
        ClientManager.sendMessage(channelId, value);
    }

    @Override
    public void setStatus(int status) {
        this.status = status;
    }
}
